For this exercise we will test your knowledge of data frames! Just follow the exercise instructions that are in bold below!
Ex 1: Recreate the following dataframe by creating vectors and using the data.frame function:
Ex 2: Check if mtcars is a dataframe using is.data.frame()
Ex 3: Use as.data.frame() to convert a matrix into a dataframe:
mat <- matrix(1:25,nrow = 5)
Ex 4: Set the built-in data frame mtcars as a variable df. We'll use this df variable for the rest of the exercises.
df <- mtcars
Ex 5: Display the first 6 rows of df
Ex 6: What is the average mpg value for all the cars?
Ex 7: Select the rows where all cars have 6 cylinders (cyl column)
Ex 8: Select the columns am,gear, and carb.
Ex 9: Create a new column called performance, which is calculated by hp/wt.
head(df)
Ex 10: Your performance column will have several decimal place precision. Figure out how to use round() (check help(round)) to reduce this accuracy to only 2 decimal places.
head(df)
Ex 10: What is the average mpg for cars that have more than 100 hp AND a wt value of more than 2.5.
Ex 11: What is the mpg of the Hornet Sportabout?
You'll get even more practice on these operations during your final project!